<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page.sync="page"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button size="small" icon="el-icon-delete" @click="handleRole" plain>菜单权限设置</el-button>
      </template>
    </avue-crud>
    <el-dialog title="角色配置" :visible.sync="box" width="40%">
      <el-tree :data="treedata" show-checkbox node-key="id" ref="tree" 
      :default-checked-keys="defaultObj" :props="treeprops">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="box = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getMenuTree,
  getRoleList,
  submitRole,
  removeRole,
  getRoleMenu,
  updateRoleMenu,
} from "@/api/system";

export default {
  data() {
    return {
      query : {},
      boxIndustry: false,
      listIndustry: [],
      defaultIndustry: [],
      propsIndustry: {
        label: "title",
        value: "key",
      },

      form: {},
      box: false,
      treedata: [],
      treeprops: {value: "id",label: "name"},
      list: [],
      defaultObj: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        searchMenuSpan: 6,
        tip: false,
        // tree: true,
        border: true,
        // index: true,
        selection: true,
        viewBtn: false,
        // dialogWidth: 300,
        // dialogHeight: 400,
        column: [
          {
            label: "Id",
            prop: "id",
            hide: true,
            addDisplay: false,
            editDisplay: false,
          },
          {
            label: "角色名称",
            prop: "roleName",
            search: true,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入角色名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "角色别名",
            prop: "roleAlias",
            search: true,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入角色别名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "角色排序",
            prop: "sort",
            type: "number",
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入角色排序",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  created() {
    getMenuTree().then((res) => {
      this.treedata = res.data.data;
    });
  },
  methods: {
    submit() {
      const menuList = this.$refs.tree.getCheckedKeys().join(",");
      updateRoleMenu(this.ids, menuList).then(() => {
        this.box = false;
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        this.onLoad(this.page);
      });
    },
    rowSave(row, done, loading) {
      submitRole(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, () => {
          loading();
        });
      },
    rowUpdate(row, index, done, loading) {
      submitRole(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          // 数据回调进行刷新
          done();
        }, () => {
          loading();
        });
      },
    rowDel(row, index, done) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return removeRole(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },

    searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
    handleRole() {
      if (this.selectionList.length !== 1) {
        this.$message.warning("只能选择一条数据");
        return;
      }
      this.defaultObj = [];
      getRoleMenu(this.ids).then((res) => {
        this.box = true;
        this.defaultObj = res.data.data;
        this.$refs.tree.setCheckedKeys(res.data.data);
      });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return removeRole(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let p =Object.assign(params, this.query);
      getRoleList(page.currentPage, page.pageSize, p).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
    },
  },
};
</script>

